<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category   Cas
 * @package    Cas_Nickname
 * @subpackage Cas_Nickname
 * @copyright  Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author     Zeed Team (http://blog.zeed.com.cn)
 * @version    SVN: $Id: $
 */

class Helper_Attachment
{
    /**
     * 申请删除资料
     * 
     * @param string $attachments 要删除的资料
     *      格式：【_id:userid】
     *      多个以半角逗号分隔
     * @param integer $userid_proposer 申请人用户 ID
     */
    public static function delete($attachments, $userid_proposer)
    {
        $res = array('status' => 0, 'error' => null, 'data' => null);
        
        try {
            /* 处理参数 */
            $attachments_arr = explode(',', $attachments);
            $timenow = Util_Timer::getMicrotime();
            
            if (! empty($attachments_arr)) {
                foreach ($attachments_arr as $k => $v) {
                    $attachment_detail = explode(':', $v);
                    
                    /* 组织数据并入库 */
                    $set_add = array(
                            'type' => 3,
                            'sn' => $attachment_detail[0],
                            'userid_proposer' => $userid_proposer,
                            'userid_verifier' => (int) $attachment_detail[1],
                            'memo' => '',
                            'status' => 0,
                            'ctime' => $timenow,
                            'mtime' => $timenow
                    );
                    $confirm_id = Archive_Model_Confirm::instance()->addForEntity($set_add);
                    
                    /* 更新资料状态 */
                    $set_update = array(
                            'status' => -4
                    );
                    Attachment_Model_Content::instance()->updateForEntity($set_update, $attachment_detail[0]);
                    
                    /* 新增一条通知消息记录 */
                    Zeed_Hook::exec('Hooks.Notify.AttachmentDeleteConfirm',
                            array(
                                    'send_userid' => $userid_proposer,
                                    'to_userid' => $attachment_detail[1],
                                    'sn' => $attachment_detail[0],
                                    'card_id' => $attachment_detail[2],
                                    'confirm_id' => $confirm_id
                            ));
                }
            }
        } catch (Zeed_Exception $e) {
            $res['status'] = $e->getCode() ? $e->getCode() : 1;
            $res['error'] = '提交失败 : ' . $e->getMessage();
        }
        
        return $res;
    }
}
